/*
 * @Author: yuki
 * @Date: 2025-10-25 16:42:49
 * @LastEditTime: 2025-11-05 19:19:14
 * @Description: 登陆注册控制器
 */
import {
  Body,
  Controller,
  Get,
  HttpException,
  Post,
  Request,
  HttpStatus
} from '@nestjs/common';
import type { Request as ExpressRequest } from 'express';

import { Public } from '@/decorator/public.decorator';
import { RegisterPipe, LoginPipe } from '@/pipe/auth.pipe';
import type { ITokenPayload } from '@/share/token.service';

import { AuthService } from './auth.service';
import { RegisterDto, LoginDto } from './dto/auth.dto';

@Controller('auth')
@Public()
export class AuthController {
  constructor(private readonly authService: AuthService) {}

  @Post('/register')
  register(@Body(RegisterPipe) data: RegisterDto) {
    return this.authService.register(data);
  }

  @Post('/login')
  login(@Body(LoginPipe) data: LoginDto) {
    return this.authService.login(data);
  }

  @Get('/test')
  profile(@Request() req?: ExpressRequest & { user: ITokenPayload }) {
    if (!req?.user) {
      throw new HttpException('未登录', HttpStatus.UNAUTHORIZED);
    }
    return req.user;
  }
}
